package com.inossem.warehousemanage_tv.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.inossem.warehousemanage_tv.R;
import com.inossem.warehousemanage_tv.bean.MatsBean;

import java.util.List;

/**
 * 仓库概览-物料列表
 *
 * @author 詹建宇
 * @time on 2019/2/14 14:09
 * @email jianyu.zhan@inossem.com
 */
public class MatsAdapter extends BaseAdapter {

    private Context context;
    private List<MatsBean.MaterialListBean> list;
    private int position;
    private OnItemClickListener listener;

    public MatsAdapter(Context context, List<MatsBean.MaterialListBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.view_ware_mats_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        MatsBean.MaterialListBean bean = list.get(position);
        viewHolder.img.setImageResource(R.mipmap.ic_logo);
        viewHolder.matNameTv.setText(bean.getMat_name());
        viewHolder.matNumTv.setText(bean.getTotal() + bean.getName_zh());

        if (this.position == position) {
            viewHolder.layout.setBackgroundResource(R.drawable.mat_select_bg);
        } else {
            viewHolder.layout.setBackground(null);
        }

        viewHolder.layout.setOnClickListener(v -> {
            if (MatsAdapter.this.position == position) return;
            MatsAdapter.this.position = position;
            if (null != listener) {
                listener.onItemClick(position, bean);
            }
            notifyDataSetChanged();
        });

        return convertView;
    }

    private class ViewHolder {
        private LinearLayout layout;
        private ImageView img;
        private TextView matNameTv, matNumTv;

        public ViewHolder(View view) {
            layout = view.findViewById(R.id.layout);
            img = view.findViewById(R.id.img);
            matNameTv = view.findViewById(R.id.matNameTv);
            matNumTv = view.findViewById(R.id.matNumTv);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position, MatsBean.MaterialListBean layerBean);
    }

}
